package cn.doitedu;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.roaringbitmap.RoaringBitmap;

import java.io.IOException;
import java.sql.SQLException;

public class StaticProfilePreSelectTest {

    public static void main(String[] args) throws IOException, SQLException {


        RoaringBitmap tagSelectUsersBitmap = RoaringBitmap.bitmapOf();
        RoaringBitmap dorisSelectUsersBitmap = RoaringBitmap.bitmapOf();


        // es的请求客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient
                        .builder(new HttpHost("doitedu", 9200, "http"))
        );

        // 用于查询参数封装的对象
        SearchRequest request = new SearchRequest("doit42_userprofile");

        IdsQueryBuilder idQuery = QueryBuilders.idsQuery().addIds(8 + "");
        RangeQueryBuilder ageQuery = QueryBuilders.rangeQuery("tag0202").gte(20).lte(28);
        MatchQueryBuilder genderQuery = QueryBuilders.matchQuery("tag0201", "female");

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(idQuery)
                .must(ageQuery)
                .must(genderQuery);


        request.source(new SearchSourceBuilder().query(boolQueryBuilder));

        // 用客户端发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        System.out.println(response.getHits().getHits().length);

        client.close();

    }


}


